package com.lagou.niancheng.lagouservicecode.controller;

import com.lagou.niancheng.lagou.service.common.CommonErrorCode;
import com.lagou.niancheng.lagou.service.common.ErrorCode;
import com.lagou.niancheng.lagou.service.common.LagouException;
import com.lagou.niancheng.lagou.service.common.Result;
import com.lagou.niancheng.lagou.service.common.ResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * com.lagou.niancheng.lagouserviceuser.controller.ExpAdvice
 *
 * @author wlz
 * @date 2020/6/28
 */
@RestControllerAdvice
public class ExpAdvice {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExpAdvice.class);

    @ExceptionHandler(LagouException.class)
    public Result lagouException(LagouException lagouException) {
        return ResultBuilder.toError(lagouException.getErrorCode(),
                lagouException.getErrorMsg(), lagouException.getParams());
    }

    @ExceptionHandler(Throwable.class)
    public Result sysException(Throwable sysException) {
        LOGGER.error("发生未知异常", sysException);
        ErrorCode errorCode = CommonErrorCode.SYSTEM_ERROR;
        return ResultBuilder.toError(errorCode);
    }
}
